#include"../include/lib_headers.h"
label* addfile_from_vfs(char *pt, node *file) {
	int counter=0, i = 0, j = 0, k = 0, match = 0, t = 0, y = 1;
	char *s, input[SIZE] = "", *split_char, *p[SIZE], copy[SIZE]="",parent[SIZE] = "";
	node *temp, *prev;

	pt = add_slash(pt);
	strcpy(parent, pt);

	
	if(find_node(l1->next, parent) != NULL && strcmp(find_node(l1->next, parent)->fd_pointer->fileType, "dir") != 0){
		remove_file(parent);
	}	

	split_char = "/";
	i = splits_directory(parent, p, split_char);


	if (l1->next == NULL) {
		j = 0;
		node *n = (node*) malloc(sizeof(node));
		fd *f= (fd*) malloc(sizeof(fd));
		n->fd_pointer=f;
		strcpy(f->fileName, p[j]);
		strcpy(f->fullPath, "/");
		strcat(f->fullPath, p[j]);
		/* strcat(f->fullPath, "/"); */
		strcat(copy, "/");
		strcat(copy, p[j]);
		/* strcat(copy, "/"); */
		l1->next = n;
		n->child = NULL;
		n->sibling = NULL;
		f->blockno= freelist_alloc();
		freelist[f->blockno]=1;
		readfile_from_vfs(f->blockno, file->fd_pointer);
		f->fileSize = file->fd_pointer->fileSize;
		strcpy(f->fileType, file_extension(file->fd_pointer->fullPath));
		j++;
		temp = n;
		insert(f);
		htable_insert(hashtable,*f);
		
		while (j < i) {
			node *n = (node*) malloc(sizeof(node));
			fd *f= (fd*) malloc(sizeof(fd));
			n->fd_pointer=f;
			strcat(copy, "/");
			strcat(copy, p[j]);
			/* strcat(copy, "/"); */
			strcpy(f->fileName, p[j]);
			strcpy(f->fullPath, copy);
			temp->child = n;
			n->child = NULL;
			n->sibling = NULL;
			f->blockno= freelist_alloc();
			freelist[f->blockno]=1;
			readfile_from_vfs(f->blockno, file->fd_pointer);
			f->fileSize = file->fd_pointer->fileSize;
			strcpy(f->fileType,file_extension(file->fd_pointer->fullPath));
			temp = n;
			j++;
			insert(f);
			htable_insert(hashtable,*f);
		}
		return l1;
	}

	temp = l1->next;
	strcpy(copy, "");
	split_char = "/";
	i = splits_directory(parent, p, split_char);
	match = 0;
	if (l1->next != NULL) {
		j = 0;
		t = 0;
		temp = l1->next;
		while (j < i) {
			while (temp != NULL) {
				/* printf( " p[%d] = %s\n ", j, p[j]); */
				if (strcmp(temp->fd_pointer->fileName, p[j]) == 0) {
					match = 1;	
					/* printf(" came inside\n"); */				 
					strcat(copy, "/");
					strcat(copy, p[j]);
					/* strcat(copy, "/"); */
					j++;
					prev = temp;
					temp = temp->child;
					if (temp == NULL) {
						t = 1;						
					}
					if(j == i){
						temp=NULL;						
						break;
					}	

				}
				if (match == 0) {
					prev = temp;
					temp = temp->sibling;
				} else if (match == 1) {
					match = 0;
					continue;
				}
			}
			/* printf("temp = %p prev = %p\n ", temp ,prev); */
			if (temp == NULL) {
				/* printf(" copy = %s\n", copy); */
				strcat(copy, "/");
				/* printf(" copy = %s\n", copy); */
				if(j == i)
					strcat(copy, p[j-1]);
				else
					strcat(copy, p[j]);
				/* printf(" copy = %s\n", copy); */
				/* strcat(copy, "/"); */
				node *n = (node*) malloc(sizeof(node));
				fd *f= (fd*) malloc(sizeof(fd));
				n->fd_pointer=f;
				n->child = NULL;
				n->sibling = NULL;
				if(j == i)
					strcpy(f->fileName, p[j-1]);
				else
					strcpy(f->fileName, p[j]);
				strcpy(f->fullPath, copy);
				/* printf("f->fileName = %s f->fullPath = %s\n", f->fileName,f->fullPath); */			
				f->blockno= freelist_alloc();
				freelist[f->blockno]=1;
				readfile_from_vfs(f->blockno, file->fd_pointer);
				f->fileSize = file->fd_pointer->fileSize;
				/* printf(" file_extension(pt) = %s pt = %s\n ", file_extension(pt), pt); */
				strcpy(f->fileType,file_extension(file->fd_pointer->fullPath));
				/* printf(" f->fullPath = %s f->fileName = %s ", f->fullPath, f->fileName); */
				if (t == 1) { 
					prev->child = n;
				} else if (t == 0) { 
					prev->sibling = n;
					t = 1;
				}
				j++;
				temp = n;
			insert(f);	
			htable_insert(hashtable,*f);
			}			
		}
		return l1;
	}
}

